<?php
class user extends Activity {
    function loginTask(){
        $result=array();
        View::displayAsHtml($result, "user-login.php");
    }

    function resetTask(){
        $result=array();
        View::displayAsHtml($result, "user-reset.php");
    }

    function loginSubmitTask($user, $pass){
        $userMode=UserModule::init("user");
        $loginRe=$userMode->login($user, $pass);
        if($loginRe->state==DataMessage::STATE_SUCCESS){
            if(!empty($_SESSION['user_login_call_back'])){
                $url=$_SESSION['user_login_call_back'];
                unset($_SESSION['user_login_call_back']);
            }else{
                $url=WebRouter::init()->getPage('home','index');
            }
            header("Location:".$url);
            exit();
        }else{
            $result=array("message"=>$loginRe->message,);
            View::displayAsHtml($result, "plugin/state/tips.php");
        }
    }

    // 检测手机号是不是可以用来注册
    //reg reset
    function checkTelTask($tel,$type='reg'){
        $userMode=UserModule::init("user");
        if(!$userMode->checkTelFormatValid($tel)){
            $re=new DataMessage(DataMessage::STATE_ERROR, "手机号格式错误");
        }elseif($userMode->checkUsername($tel)){
            if($type == 'reset'){
                $re=new DataMessage(DataMessage::STATE_SUCCESS, "手机号可以被重置");
            }else{
                $re=new DataMessage(DataMessage::STATE_ERROR, "手机号已经被注册");
            }
        }else{
            if($type == 'reset'){
                $re=new DataMessage(DataMessage::STATE_ERROR, "手机号不存在");
            }else{
                $re=new DataMessage(DataMessage::STATE_SUCCESS, "手机号可以使用");    
            }
            
        }
        View::displayDataMessage($re);
    }


    function registerSubmitTask($tel, $pass, $code){
        $userMode=UserModule::init("user");
        $codeRe=$userMode->checkCode($tel, $code);
        if($codeRe->state== DataMessage::STATE_SUCCESS &&
            $userMode->checkTelFormatValid($tel) &&
            !$userMode->checkUsername($tel)){
            $userId=$userMode->register($tel, $pass);
            if($userId!=-1){
                $userMode->login($tel, $pass);
                $addToUserInfo=UserMode::init()->add($userId);
                if($addToUserInfo!=-1){
                    $url=WebRouter::init()->getPage('my','index');
                    $result=array("message"=>"注册成功","url"=>$url);
                }else{
                    $result=array("message"=>"注册失败");
                }
            }else{
                $result=array("message"=>"注册失败");
            }
        }else{
            $message= $codeRe->state==DataMessage::STATE_SUCCESS? "手机号格式不对": $codeRe->title;
            $result=array("message"=>$message,);
        }
        View::displayAsHtml($result, "plugin/state/tips.php");
    }

    function resetSubmitTask($tel, $pass, $code){
        $userMode=UserModule::init("user");
        $codeRe=$userMode->checkCode($tel, $code);
        if($codeRe->state== DataMessage::STATE_SUCCESS &&
            $userMode->checkTelFormatValid($tel) &&
            $userMode->checkUsername($tel)){
            $re=$userMode->resetPass($tel,$pass);
            if($re){
                $url=WebRouter::init()->getPage('user','login');
                $result=array("message"=>"重置成功","url"=>$url);
            }else{
                $result=array("message"=>"重置失败");
            }
        }else{
            $message=$codeRe->state== DataMessage::STATE_SUCCESS? $codeRe->title: "手机号格式不对";
            $result=array("message"=>$message,);
        }
        View::displayAsHtml($result, "plugin/state/tips.php");
    }

    function loginoutTask(){
        $userMode=UserModule::init("user");
        $userMode->logout();
        $url=WebRouter::init()->getPage('user','login');
        $result=array("message"=>"退出登录成功","url"=>$url);
        View::displayAsHtml($result, "plugin/state/tips.php");
    }
}